package com.debao;


import com.debao.common.core.controller.BaseController;
import com.debao.common.core.domain.AjaxResult;
import com.debao.common.core.page.TableDataInfo;
import com.debao.common.enums.OrderStatusEnum;
import com.debao.common.utils.SecurityUtils;
import com.debao.system.domain.CourseCardOrder;
import com.debao.system.domain.GoodsOrder;
import com.debao.system.service.ICourseCardOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 课程订单
 */
@RestController
@RequestMapping("/api/courseOrder")
@CrossOrigin
public class ApiCourseOrder extends BaseController {

    @Autowired
    private ICourseCardOrderService cardOrderService;

    /**
     * 课程订单
     *
     * @param courseCardOrder
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(CourseCardOrder courseCardOrder) {
        courseCardOrder.setUserId(SecurityUtils.getUserId());
        startPage();
        return getDataTable(cardOrderService.selectCourseCardOrderList(courseCardOrder));
    }

    /**
     * 课程订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public AjaxResult info(@PathVariable("id") Long id) {
        return success(cardOrderService.selectCourseCardOrderById(id));
    }

    /**
     * 根据订单号获取课订单详情
     */
    @GetMapping("/infoByOrderNo/{orderNo}")
    public AjaxResult infoByOrderNo(@PathVariable("orderNo") String orderNo) {
        return success(cardOrderService.selectCourseCardOrderByOrderNo(orderNo));
    }

    /**
     * 订单是否支付成功
     *
     * @param orderNo
     * @return
     */
    @GetMapping("/isPaySuccess/{orderNo}")
    public AjaxResult isPaySuccess(@PathVariable("orderNo") String orderNo) {
        return AjaxResult.success(cardOrderService.isPaySuccess(orderNo));
    }

    /**
     * 取消支付
     */
    @PostMapping("/cancelPay/{orderNo}")
    public AjaxResult cancelPay(@PathVariable("orderNo") String orderNo) {
        CourseCardOrder courseCardOrder = cardOrderService.selectCourseCardOrderByOrderNo(orderNo);
        if (Objects.isNull(courseCardOrder)) {
            return error("订单不存在");
        }
        if (!courseCardOrder.getStatus().equals(OrderStatusEnum.None.name())) {
            return error("订单已处理");
        }
        courseCardOrder.setStatus(OrderStatusEnum.Cancel.name());
        int row = cardOrderService.updateCourseCardOrder(courseCardOrder);
        if (row == 1) {
            return AjaxResult.success("取消成功");
        }
        return error("取消失败");
    }

}
